Fail TestServer requests as soon as the client cancels - #69106
Open
georgehotca wants to merge 2 commits into
Open
Fail TestServer requests as soon as the client cancels#69106georgehotca wants to merge 2 commits into
georgehotca wants to merge 2 commits into
Conversation
HttpContextBuilder registered the client's cancellation token only to abort the server side. The task handed back to the client stayed pending until the application finished, so an application that never checks RequestAborted kept the client waiting past its own cancellation. A real server fails the client's request the moment the token fires. TestServer now does the same: the cancellation registration completes the response task with an OperationCanceledException. The application keeps running and still sees the abort through RequestAborted. Fixes dotnet#5938
Contributor
Author
|
@dotnet-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fail TestServer requests as soon as the client cancels
TestServer clients now fail at cancellation instead of waiting for the app.
Description
HttpContextBuilder.SendAsyncregisters the client's cancellation token, but the registration aborted the server side and nothing else: it firedRequestAbortedand aborted the response stream. The task returned to the client stayed pending until the application delegate finished. An application that never checksRequestAbortedkept the client waiting past its own cancellation, which is the repro in #5938. Against a real server,HttpClientthrowsTaskCanceledExceptionthe moment the token fires.The registration now also completes the response task with an
OperationCanceledExceptionthat carries the token.HttpClientsurfaces it asTaskCanceledException, the same as against Kestrel. The application keeps running in the background and continues to see the abort throughRequestAborted.Scope, measured against the issue thread
@Tratcher's rule from the thread: the token governs the duration of
SendAsynconly, and underResponseHeadersReadit must never reach the response stream. The change keeps to that. It touches the response task alone, which is whatSendAsyncawaits; once that task has a result the registration has nothing left to do.The thread also pointed at content buffering. On current
main,HttpClientalready passes its token intoLoadIntoBufferAsync, so that phase honors cancellation without any change here. I measured four cases with a 1-second token against a 2-second app, onmainand with this change:ResponseContentRead(the issue's repro)TaskCanceledExceptionafter 2062 msTaskCanceledExceptionafter 1004 msResponseHeadersReadTaskCanceledExceptionafter 998 msResponseContentReadTaskCanceledExceptionafter 1001 msResponseHeadersReadOnly the pending-response phase changes. Buffering already honored the token, and
ResponseHeadersReadnever applied it to later reads.Verification
ClientCancellationThrowsWithoutWaitingForApplicationinTestClientTests. The app signals it has started, then awaits a task it never completes. The client cancels, and the request task must throwOperationCanceledException. Without the fix the test failed at the 5-second guard withTimeoutException; with the fix it passes.ClientCancellationAbortsRequestcovers the case where the application observes the token. It is unchanged and still passes.Microsoft.AspNetCore.TestHost.Testsrun: 150 passed, 0 failed, 0 build warnings.No public API changes.
Fixes #5938